package org.zhaozhen.pet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerInterceptor;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.util.AjaxResult;
import org.zhaozhen.basic.util.LoginContext;
import org.zhaozhen.basic.util.PageList;
import org.zhaozhen.pet.domain.Pet;
import org.zhaozhen.pet.query.PetQuery;
import org.zhaozhen.pet.service.IPetService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 宠物管理列表分页&前端主站宠物页面列表数据分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage2(@RequestBody PetQuery query,HttpServletRequest request){
            PageList<Pet> petPageList = petService.queryPage2(query,LoginContext.getCurrentLogin(request));
            return petPageList;
    }
    @PostMapping("/pets")
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        PageList<Pet> petPageList = petService.queryPage(query);
        return petPageList;
    }

    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
           petService.onsale(ids);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 前端页面商品详细查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
        return petService.loadById(id);
    }
}
